﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using XQ.WeChatOfficialAccount.DAL.Model;

namespace XQ.WeChatOfficialAccount.DAL.DAL
{
    public class IndexDal
    {
        WeChatOfficialAccountEntities ce;

        /// <summary>
        /// 返回一级菜单
        /// </summary>
        /// <returns></returns>
        public List<SysMenusExModel> GetParentMenus(int userId)
        {
            try
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    string sql = @"select distinct Sys_Menus.*
                            from Sys_User_Roles  
                            left join Sys_Role_Menu_Btns   
                            left join Sys_Menus  
                            on Sys_Menus.ID=Sys_Role_Menu_Btns.MenuID 
                            on Sys_User_Roles.RoleID=Sys_Role_Menu_Btns.RoleId 
                            where Sys_User_Roles.UserID=@userId and Sys_Menus.IsVisible=1 and  Sys_Menus.ParentID is null
                            order by Sys_Menus.Sortnum";
                    object[] par = { new SqlParameter("@userId", userId) };
                    return ce.Database.SqlQuery<SysMenusExModel>(sql, par).ToList();

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 返回子项
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<SysMenusExModel> GetSubItem(int pid, int userId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                string sql = @"select distinct Sys_Menus.*
                            from Sys_User_Roles  
                            left join Sys_Role_Menu_Btns   
                            left join Sys_Menus  
                            on Sys_Menus.ID=Sys_Role_Menu_Btns.MenuID 
                            on Sys_User_Roles.RoleID=Sys_Role_Menu_Btns.RoleId 
                            where Sys_User_Roles.UserID=@userId and Sys_Menus.IsVisible=1 and  Sys_Menus.ParentID =@pid
                            order by Sys_Menus.Sortnum";
                object[] par = { new SqlParameter("@userId", userId), new SqlParameter("@pid", pid) };
                List<SysMenusExModel> result = ce.Database.SqlQuery<SysMenusExModel>(sql, par).ToList();
                return result;
            }
        }



    }
}
